package com.heima.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.common.PageResult;
import com.heima.mapper.NodeMapper;
import com.heima.pojo.User;
import com.heima.pojo.VendingMachine;
import com.heima.mapper.VendingMachineMapper;
import com.heima.pojo.dto.VendingMachineDto;
import com.heima.pojo.vo.NodeVo;
import com.heima.pojo.vo.RegionVo;
import com.heima.pojo.vo.VendingMachineVo;
import com.heima.service.INodeService;
import com.heima.service.IUserService;
import com.heima.service.IVendingMachineService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author xjy
 * @since 2024-06-06
 */
@Service
public class VendingMachineServiceImpl extends ServiceImpl<VendingMachineMapper, VendingMachine> implements IVendingMachineService {
    @Autowired
    private VendingMachineMapper vendingMachineMapper;
    @Autowired
    private INodeService nodeService;
    @Autowired
    private IUserService userService;
    @Autowired
    private NodeMapper nodeMapper;

    /**
     * 分页查询售货机列表
     *
     * @param innerCode 设备软编号
     * @return
     */
    @Override
    public PageResult<VendingMachineVo> search(PageResult<VendingMachineVo> pageResult, String innerCode) {
        Page<VendingMachineVo> page = new Page<>(pageResult.getPageIndex(), pageResult.getPageSize());
        vendingMachineMapper.search(page, innerCode);
        List<VendingMachineVo> records = page.getRecords();
        List<Long> longList = records.stream().map(item -> item.getNodeId()).collect(Collectors.toList());
        List<NodeVo> nodeVos = nodeMapper.batchSelectByIds(longList);
        records.forEach(record -> {
            nodeVos.forEach(nodeVo -> {
                if (nodeVo.getId().equals(record.getNodeId())) {
                    record.setNode(nodeVo);
                }
            });
        });
        return pageResult.of(page, VendingMachineVo.class);
    }

    /**
     * 添加售货机
     *
     * @param vendingMachineDto 售货机信息
     * @return
     */
    //TODO 添加售货机的售货机软编号、所在城市ID、区域id未添加
    @Override
    public Boolean addVendingMachine(VendingMachineDto vendingMachineDto) {
        NodeVo detailById = nodeService.getDetailById(vendingMachineDto.getNodeId());
        VendingMachine vm = BeanUtil.toBean(vendingMachineDto, VendingMachine.class);
        User byId = userService.getById(vm.getCreateUserId());
        vm.setCreateTime(LocalDateTime.now());
        vm.setUpdateTime(LocalDateTime.now());
        vm.setRegionId(Long.valueOf(detailById.getRegionId()));
        vm.setAreaId(Integer.valueOf(detailById.getAreaCode()));
        vm.setBusinessId(detailById.getBusinessId());
        vm.setOwnerId(detailById.getOwnerId());
        vm.setOwnerName(detailById.getOwnerName());
        vm.setCreateUserId(Long.valueOf(detailById.getCreateUserId()));
        vm.setCreateUserName(byId.getUserName());
        return save(vm);
    }
}
